package com.gp.cyg.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpConfig;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.gp.cyg.exception.CustomException;
import com.gp.cyg.task.CygBuyTask;
import com.gp.cyg.task.MyDelay;
import com.gp.cyg.utils.ThreadPoolUtils;
import com.gp.cyg.vo.CalculateVO;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * CygController
 *
 * @author gsp
 * @date 2022/9/8
 */
@RestController
@RequestMapping("/v1")
@Slf4j
public class CygController {
    @Autowired
    private ThreadPoolUtils threadPoolUtils;

    public HashMap<String, String> resultMap;

    private DelayQueue<MyDelay> delayQueue;

    @Value("${delayTime}")
    private Long delayTime;

    @Value("${ngrokUrl}")
    private String ngrokUrl;
    @Autowired
    private CygBuyTask cygBuyTask;

    public String goodsCode;

    @PostConstruct
    public void initResultMap() {
        resultMap = new HashMap<>();
        resultMap.put("code", "200");
        delayQueue = new DelayQueue<>();
    }

    @PostMapping("/createTask")
    public HashMap<String, String> createTask(@RequestBody HashMap<String, String> params) throws CustomException, IOException, InterruptedException {
        cygBuyTask.checkRemainTime(params.get("goodsCode"));
        goodsCode = params.get("goodsCode");
        delayTime=StrUtil.isBlank(params.get("delayTime"))?2100L:Long.parseLong(params.get("delayTime"));
        ThreadPoolExecutor threadPoolExecutor = threadPoolUtils.getThreadPoolExecutor();
        threadPoolExecutor.submit(
                () -> {
                    String goodsCode=params.get("goodsCode");
                    String proxyUrl=params.get("proxyUrl");
                    int count=Integer.parseInt(params.get("count"));
                    String cookie=params.get("cookie");
                    //购买延时任务
                    CalculateVO calculateVO = cygBuyTask.calculateRemainTime(goodsCode);
                    ConcurrentLinkedQueue<Proxy> concurrentLinkedQueue=new ConcurrentLinkedQueue<>();
                    //获取代理ip
                    if (StrUtil.isNotBlank(proxyUrl)) {
                        //批量获取代理ip延时任务
                        cygBuyTask.getIpByBatch(proxyUrl,calculateVO,delayQueue,concurrentLinkedQueue,count,delayTime);
                        while (!concurrentLinkedQueue.isEmpty()) {
                            Integer integer = cygBuyTask.beginBuyTaskMuchIp(proxyUrl, delayQueue, goodsCode, cookie, concurrentLinkedQueue, calculateVO);
                            if (integer==-1){
                                break;
                            }
                        }
                    }else {
                        cygBuyTask.beginBuyTaskSingleIp(proxyUrl,delayQueue,goodsCode,cookie,calculateVO,delayTime);
                    }
                });
        resultMap.put("msg", "任务挂起成功");
        return resultMap;

    }

    @GetMapping("/getTasks")
    public HashMap<String, String> getTasks() {
        log.info("ngrokUrl" + ngrokUrl);
        resultMap.put("msg", goodsCode + "商品任务正在执行");
        return resultMap;
    }


}
